#! /usr/bin/env bash

# Copyright (c) 2006-2015, Universities Space Research Association (USRA).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the Universities Space Research Association nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY USRA ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL USRA BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cleanFld="clean"
bookTemplate="./PictureMan.book"
if [ "$1" = "clean" ]; then
	if [ -d $cleanFld ]; then
		rm $cleanFld -r
	fi
	if [ -e $bookTemplate ]; then
		rm $bookTemplate
	fi
	echo "cleaned /$cleanFld and $bookTemplate"
	exit 1
fi

parse="./parse"
if [ -e $parse ]; then
	:
else 
	echo "$parse does not exists"
	exit 1
fi 

process="./process"
if [ -e $process ]; then
	:
else 
	echo "$process does not exists"
	exit 1
fi 

if [ -e $bookTemplate ]; then
	echo "Warning: Re-writing existing $bookTemplate"
fi

echo '#HTMLDOC 1.8.27 
-t pdf14 -f "plexil-manual.pdf" --book --toclevels 4 --no-numbered --toctitle "Table of Contents" --title --textcolor #000000 --linkstyle underline --size Universal --left 1.00in --right 0.50in --top 0.50in --bottom 0.50in --header .c. --header1 d.. --footer ..1 --nup 1 --tocheader .C. --tocfooter ..i --portrait --color --no-pscommands --no-xrxcomments --compression=1 --jpeg=90 --fontsize 11.0 --fontspacing 1.2 --headingfont Helvetica --bodyfont Times --headfootsize 11.0 --headfootfont Helvetica --charset iso-8859-1 --links --embedfonts --pagemode outline --pagelayout single --firstpage c1 --pageeffect none --pageduration 10 --effectduration 1.0 --no-encryption --permissions all  --owner-password ""  --user-password "" --browserwidth 680 --no-strict --no-overflow' > $bookTemplate 

wget -O intro -k http://plexil.sourceforge.net/wiki/index.php/Introduction

touch plexil_links.txt
echo 'http://plexil.sourceforge.net/wiki/index.php/Introduction' >> plexil_links.txt

sed -nf ./parse intro

intro="./intro"
if [ -e $intro ]; then
	rm intro
else 
	echo "$intro does not exists"
fi 

#wget -A.jpg -x --cut-dirs=12 -r -l1 -np -i plexil_links.txt
#wget -A.png -x --cut-dirs=12 -r -l1 -np -i plexil_links.txt
wget -x --convert-links --cut-dirs=12 -i plexil_links.txt
echo
echo
echo "----downloading completed----"
echo
echo
links="./plexil_links.txt"
if [ -e $links ]; then
	rm plexil_links.txt
	#echo "" > /dev/null
else 
	echo "$links does not exists"
fi 

mkdir clean
echo
echo
echo "----Cleaning links----"
echo
echo
sf="./sourceforge.net"
if [ -d $sf ]; then
	for f in $(ls $sf); do 
		if [[ ( $f =~ ".*png" ) || ( $f =~ ".*jpg" ) ]]
		then
			echo "" > /dev/null		
		else	
			if [[ $f =~ ".*title=.*" ]]
			then
				sed -f $process $sf/$f > clean/$f
			fi
		fi
	done
else 
	echo "$sf does not exists"
fi 

names="./naming.txt"
if [ -e $names ]; then
	sed 's/[:]* <a.*plexil[/]/|/g' $names > names.txt
	rm $names
else 
	echo "$names does not exists"
fi 

echo
echo
echo "----Formatting links----"
echo
echo
names="names.txt"
	
chapters=$(wc -l $names | sed 's/ .*//')

if [ -d $cleanFld ]; then
	while read line
	do 
	    grab=$line
	    messy=$(echo $grab | sed 's/.*|//')	    
	    clean=$(echo $grab | sed 's/|.*//')
	    if [ -e $cleanFld/$messy ]; then
		mv $cleanFld/$messy $cleanFld/"$clean".html
	    else 
		echo "Error: $messy does not exists"
	     fi 
	done < $names
	if [ -d $sf ]; then
		rm $sf/*
		rmdir $sf
	fi
else 
	echo "Error: $cleanFld does not exists"
fi 

if [ -e $names ]; then
	while read line
	do
	    echo clean/$(echo $line | sed 's/|.*//').html >> $bookTemplate
	done < $names
	rm $names
fi	

exec htmldoc --batch $bookTemplate
